/**
 * 分页工具
 * * 2018年8月28日 
 * 
 * * bylar nico@coser.pub
 */
let __NEXT_PAGE__ = {};
const defaultPage = require('../data/config.js').nextpage.defaultPage;

function _init(that){
  let asLib = {};
  that._saveData("__NEXT_PAGE__",{});
  __NEXT_PAGE__ = that.data.__NEXT_PAGE__;
  /**
   * _NP_appendApi 增加一列api接口
   */
  that._NP_appendApi = function (apiName, moduleName, format){
    that._saveData("__NEXT_PAGE__/requestData/page/" + apiName, defaultPage);
    that._saveData("__NEXT_PAGE__/requestData/data/" + apiName, {});
    that._saveData("__NEXT_PAGE__/data/" + apiName, {});
    
    that._saveData("__NEXT_PAGE__/module/" + apiName, moduleName);
    that._saveData("__NEXT_PAGE__/format/" + apiName, format || false); 

    that._saveData("__NEXT_PAGE__/nowSwitch", apiName);

    that._saveData("__NEXT_PAGE__/resultHook/" + apiName, function(){});    
  }
  /** 
   * _NP_setFormat 填入数据
   */
  that._NP_setFormat = function (apiName, formatFunction) {
    that._saveData("__NEXT_PAGE__/format/" + apiName, formatFunction);
  }
  /** 
  * _NP_setData 填入数据
  */
  that._NP_setData = function (apiName, data) {
    that._saveData("__NEXT_PAGE__/data/" + apiName, data);
  }
  /**
   * 
   */
  that._NP_as = function (apiName, asName){
    asLib[apiName] = asName;
  }
  /** 
   * _NP_setRequestData 填入数据
   */
  that._NP_setRequestData = function(apiName, data){
    that._saveData("__NEXT_PAGE__/requestData/data/" + apiName, data);
  }
  /** 
   * _NP_getRequestData 获取数据数据
   */
  that._NP_getRequestData = function (apiName) {
    return that._getData("__NEXT_PAGE__/requestData/data/" + apiName);
  }
  /** 
   * _NP_setPage 填入分页信息
   */
  that._NP_setPage = function (apiName, page) {
    that._saveData("__NEXT_PAGE__/requestData/page/" + apiName, page);
  }
  /** 
   * _NP_getPage 获取分页信息
   */
  that._NP_getPage = function (apiName) {
    return that._getData("__NEXT_PAGE__/requestData/page/" + apiName);
  }
  /**
   * _NP_nextPage 函数
   */
  that._NP_nextPage = function (name, reload) {
    let that = this;    
    __NEXT_PAGE__ = that.data.__NEXT_PAGE__;    
    if (reload) {
      that._NP_setPage(name +"/number",0);
    }
    let page = that._NP_getPage(name);
    function listsSuccess(data){
      __NEXT_PAGE__.resultHook[name](data);
      //从服务器获取列表
      let lists = data.lists || [];
      for (let i = 0; i < lists.length; i++) {
        if (__NEXT_PAGE__.format[name]) lists[i] = that._format(__NEXT_PAGE__.format[name])(lists[i]);
      }
      // 数据更新逻辑
      if (page['number'] == 0) {
        that._saveData('__NEXT_PAGE__/data/' + name, lists);
        if (asLib[name]) that._saveData(asLib[name], lists);
        
        wx.stopPullDownRefresh();
      } else {
        let old = that.data.__NEXT_PAGE__.data[name];
        that._saveData('__NEXT_PAGE__/data/' + name, old = old.concat(lists));
        if (asLib[name]) that._saveData(asLib[name], old);
        
        wx.stopPullDownRefresh();
      }
      // 页数更新逻辑
      if (lists.length < page['size']) {
        page['number'] = -1;
      } else {
        page['number'] += 1;
      }
      that._saveData('__NEXT_PAGE__/requestData/page/' + name, page);
    }
    if (page['number'] == -1) {
      wx.showToast({ title: '没有更多了哦', icon: "none" })
    } else {
      let data = that._NP_getRequestData(name) || {};
      let send_page = that._cloneObject(page);
      send_page["number"] += 1;
      data['page'] = send_page;
      that._request(__NEXT_PAGE__.module[name])[name](data, { success: listsSuccess });
    }
  };
  that.onPullDownRefresh = function () {
    let __NEXT_PAGE__ = this.data.__NEXT_PAGE__;
    let apiName = __NEXT_PAGE__.nowSwitch;
    that._NP_nextPage(apiName, true);
  };
  that.onReachBottom = function () {
    let __NEXT_PAGE__ = this.data.__NEXT_PAGE__;
    let apiName = __NEXT_PAGE__.nowSwitch;
    that._NP_nextPage(apiName);
  };
}

module.exports._init = _init